﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_KQKT
{
    public partial class FormBaoCaoMau1 : MyFromBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            lblNotice.Visible = false;
            if (!IsPostBack)
            {
                BindDataToDropDownList(); ;
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
        }

        protected void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau1");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);
        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void BindDataToDropDownList()
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='KyBaoCao'");
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng lựa chọn đơn vị muốn xem báo cáo";
            }
            else
            {
                List<SqlParameter> lstParams = new List<SqlParameter>();

                if (chkTongHop.Checked)
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, IDDonVi + ";"));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT  B.TenDM AS DMCha,A.TenDM,A.BoiDen,A.LaToChuc,SUM(bc.F1) AS F1 , " +
                                      "SUM(bc.F2) AS F2 ,SUM(bc.F3) AS F3 ,SUM(bc.F4) AS F4 ,SUM(bc.F5) AS F5 ,SUM(bc.F6) AS F6 , " +
                                      "SUM(bc.F7) AS F7 ,SUM(bc.F8) AS F8 ,SUM(bc.F9) AS F9 ,SUM(bc.F10) AS F10 ,SUM(bc.F11) AS F11 , " +
                                      "SUM(bc.F12) AS F12 ,SUM(bc.F13) AS F13 ,SUM(bc.F14) AS F14 , "+
                                      "SUM(bc.F15) AS F15 ,SUM(bc.F16) AS F16 ,SUM(bc.F17) AS F17 ,SUM(bc.F18) AS F18 ,SUM(bc.F19) AS F19, "+
                                      "SUM(bc.F20) AS F20 ,SUM(bc.F21) AS F21 ,SUM(bc.F22) AS F22 ,SUM(bc.F23) AS F23,'' AS Ghichu " +
                                      "FROM    dbo.tblDanhMucBaoCao B " +
                                      "INNER JOIN dbo.tblDanhMucBaoCao A ON A.FatherID = B.ID " +
                                      "INNER JOIN dbo.tblBaoCaoMau1_A bc ON bc.TenBaoCaoID_DM = A.ID " +
                                      "WHERE KyBaoCaoID_DM = @KyBaoCao AND bc.DonViID IN ( SELECT  ID " +
                                      "FROM tblDonVi WHERE   parentID LIKE '%' + @parentID + '%' ) " +
                                      "GROUP BY B.TenDM,A.TenDM,A.LaToChuc,A.BoiDen";
                }
                else
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT B.TenDM AS DMCha,A.TenDM,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,A.BoiDen,A.LaToChuc " +
                              "FROM dbo.tblBaoCaoMau1_A M1 INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                              "INNER JOIN dbo.tblDanhMucBaoCao B ON A.FatherID = B.ID WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID";

                }
                MyReportParams = lstParams;
                MyReportSQLParam = "SELECT  BC.CM_SoBaoCao AS SoBaoCao,BC.CM_TenBaoCao AS TenBaoCao ,BC.CreateDate AS TuNgay,BC.UpdateDate AS DenNgay,BC.CM_NguoiLapBieu AS NguoiLapBaoCao," +
                                   "BC.CM_ThuTruong AS BKTDU,CM_ThoiGianBaoCao AS NgayBaoCao,DV.TenDV FROM dbo.tblBaoCaoMau1_A BC ,tblDonVi DV "+
                                   "WHERE  BC.DonViID = DV.ID AND BC.KyBaoCaoID_DM = @KyBaoCao AND BC.DonViID = @DonViID AND BC.IsCommon = 1";
                string UnionParam = " UNION ALL SELECT  '','',GETDATE(),GETDATE(),'','',GETDATE(),N'" + trvDonVi.SelectedNode.Text + "' WHERE NOT EXISTS (" + MyReportSQLParam + ")";
                MyReportSQLParam += UnionParam;
                MyReportName = "rpt01BaoCaoKiemTraDangVienVaToChucDang.rpt";
                MyTableName = "tblBaoCaoMau1_A";
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportView.aspx');", true);


            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}
